﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using KeepAnimals.BLL;
using KeepAnimals.UI;

namespace KeepAnimals.UI.UserControls
{
    public delegate void Action(string id);
     
    public partial class UserControlBackground : UserControl
    {

        public UserControlBackground()
        {
            InitializeComponent();
            //var departments = Department.GetListOfAllDepartments();
            //LoadObject(departments, panelContentObject);
        }

        private void UserControlBackground_Load(object sender, EventArgs e)
        {
            
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            var departments = Department.GetListOfAllDepartments();
            panelContentObject.Controls.Clear();
            LoadObject(departments, panelContentObject);
        }

        public void LoadObject(List<Department> departments, Panel panel)
        {
            int x = 0;
            int y = 0;
            const int NBOBJECTLIGNE = 4; //nombre d'object par ligne
            //const int WIDTHOBJECT = 50;
            int mainContainWidth = panel.Width;
            int taille = mainContainWidth / NBOBJECTLIGNE;
            

            double nbObjectTotal = departments.Count; // nombre objet dans la base
            int compteurObject = 1;
            double result = nbObjectTotal / NBOBJECTLIGNE;
            int nbLigne = roundUp(result);

            for (int i = 0; i < nbLigne; i++)
            {


                for (int j = 1; j <= NBOBJECTLIGNE; j++)
                {
                    if (j <= NBOBJECTLIGNE && compteurObject <= nbObjectTotal)
                    {
                        foreach (var department in departments)
                        {
                            compteurObject++;
                            var pix = new Button();

                            pix.Text = "Département " + department.Id.ToString();
                            pix.TextAlign = System.Drawing.ContentAlignment.TopCenter;
                            pix.BackgroundImage = KeepAnimals.UI.Properties.Resources.ImgDepartment1;
                            pix.BackgroundImageLayout = ImageLayout.Stretch;

                            //pix.BackColor = Color.Red;
                            pix.Width = taille;
                            pix.Height = taille;
                            pix.Left = x;
                            pix.Top = y;
                            
                            x += taille;
                            string id = department.Id.ToString();
                            panel.Controls.Add(pix);
                            pix.Name = department.Id.ToString();
                            pix.Click += pictureBox_Click;
                           
                        }
                        
                    }
                    else
                    {
                        break;
                    }
                }
                x = 0;
                y += taille;
            }
        }

        public int roundUp(double nombre)
        {
            int res = 0;

            if (Convert.ToInt32(nombre) == nombre)
            {
                res = Convert.ToInt32(nombre);
            }
            else
            {
                res = Convert.ToInt32(nombre) + 1;
            }

            return res;
        }



        private void pictureBox_Click(object sender, EventArgs e)
        {
            var dep = (Button)sender;
            var iddep = dep.Name;
            FormBoxOfDapartment p = new FormBoxOfDapartment(iddep);
            p.WindowState = FormWindowState.Maximized;
            p.Text = "KeepAnimals";
            p.Show();
            //p.Text = iddep.ToString();
        }

        private void pictureBox8_Click(object sender, EventArgs e)
        {
            var animaux = Animal.GetListOfAllAnimals();
            panelContentObject.Controls.Clear();
            LoadAllAnimaux(animaux, panelContentObject);
        }

        public void LoadAllAnimaux(List<Animal> animals, Panel panel)
        {
            int x = 0;
            int y = 0;
            const int NBOBJECTLIGNE = 4; //nombre d'object par ligne
            //const int WIDTHOBJECT = 50;
            int mainContainWidth = panel.Width;
            int taille = mainContainWidth / NBOBJECTLIGNE;


            double nbObjectTotal = animals.Count; // nombre objet dans la base
            int compteurObject = 1;
            double result = nbObjectTotal / NBOBJECTLIGNE;
            int nbLigne = roundUp(result);

            for (int i = 0; i < nbLigne; i++)
            {


                for (int j = 1; j <= NBOBJECTLIGNE; j++)
                {
                    if (j <= NBOBJECTLIGNE && compteurObject <= nbObjectTotal)
                    {
                        foreach (var animal in animals)
                        {
                            compteurObject++;
                            var pix = new Button();
                            pix.TextAlign = System.Drawing.ContentAlignment.TopCenter;
                            pix.Text = "Identifiant " + animal.Id.ToString();
                            if (animal.IdCategory == Animal.ANIMAL_CAT)
                            {
                                pix.BackgroundImage = KeepAnimals.UI.Properties.Resources.ImgCat1;
                            }
                            else
                            {
                                pix.BackgroundImage = KeepAnimals.UI.Properties.Resources.ImgDog1;
                            }
                            pix.BackgroundImageLayout = ImageLayout.Stretch;

                            //pix.BackColor = Color.Red;
                            pix.Width = taille;
                            pix.Height = taille;
                            pix.Left = x;
                            pix.Top = y;

                            x += taille;
                            string id = animal.Id.ToString();
                            
                            panel.Controls.Add(pix);
                            pix.Name = animal.Id.ToString();
                            pix.Click += ViewInfoAnimal;


                        }

                    }
                    else
                    {
                        break;
                    }
                }
                x = 0;
                y += taille;
            }
        }

        private void ViewInfoAnimal(object sender, EventArgs e)
        {
            var dep = (Button)sender;
            var iddep = dep.Name;
            FormViewAnimal p = new FormViewAnimal(iddep);
            p.Show();
            //p.Text = iddep.ToString();
        }
    }
}
